package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.system.commodity.info.SupplierSystemOrderCommodityDetailInfoDTO;
import com.ctshk.rpc.booking.dto.system.commodity.info.SupplierSystemOrderCommodityInfoDTO;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderAdmissionTicketDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderCommodity;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderCommodityDetail;
import com.ctshk.rpc.booking.entity.SupplierSystemOrderLocalFunDetail;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderCommodityDetailMapper;
import com.ctshk.rpc.booking.mapper.SupplierSystemOrderCommodityMapper;
import com.ctshk.rpc.booking.req.system.commodity.add.SupplierSystemOrderCommodityAddReq;
import com.ctshk.rpc.booking.req.system.commodity.add.SupplierSystemOrderCommodityDetailAddReq;
import com.ctshk.rpc.booking.req.system.commodity.edit.SupplierSystemOrderCommodityEditReq;
import com.ctshk.rpc.booking.service.ISupplierSystemOrderCommodityService;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 系统内商品基础信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-25
 */
@DubboService
public class SupplierSystemOrderCommodityServiceImpl implements ISupplierSystemOrderCommodityService {
    @Autowired
    SupplierSystemOrderCommodityMapper supplierSystemOrderCommodityMapper;
    @Autowired
    SupplierSystemOrderCommodityDetailMapper supplierSystemOrderCommodityDetailMapper;

    @Override
    public void add(Long id, Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderCommodityAddReq> supplierSystemOrderCommodityAddReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderCommodityAddReqList)) {
            List<SupplierSystemOrderCommodity> supplierSystemOrderCommodityList = new ArrayList<>();
            supplierSystemOrderCommodityAddReqList.forEach(trainAddReq -> {
                SupplierSystemOrderCommodity supplierSystemOrderCommodity = EntityUtil.copy(trainAddReq, SupplierSystemOrderCommodity.class);
                supplierSystemOrderCommodity.setId(SnowflakeIdWorker.nextId());
                supplierSystemOrderCommodity.setOrderId(id);
                supplierSystemOrderCommodity.setCreateId(userId);
                supplierSystemOrderCommodity.setGmtCreate(nowDateTime);
                supplierSystemOrderCommodity.setModifiedId(userId);
                supplierSystemOrderCommodity.setGmtModified(nowDateTime);
                supplierSystemOrderCommodity.setIsDeleted(IsDeletedCode.NO.getCode());
                supplierSystemOrderCommodityList.add(supplierSystemOrderCommodity);

                //添加旅客
                List<SupplierSystemOrderCommodityDetailAddReq> supplierSystemOrderCommodityDetailAddReqList = trainAddReq.getSupplierSystemOrderCommodityDetailAddReqList();
                if (CollectionUtils.isNotEmpty(supplierSystemOrderCommodityDetailAddReqList)) {
                    List<SupplierSystemOrderCommodityDetail> supplierSystemOrderCommodityDetails = new ArrayList<>();
                    supplierSystemOrderCommodityDetailAddReqList.forEach(trainTouristAdd -> {
                        SupplierSystemOrderCommodityDetail supplierSystemOrderCommodityDetail = EntityUtil.copy(trainTouristAdd, SupplierSystemOrderCommodityDetail.class);
                        supplierSystemOrderCommodityDetail.setId(SnowflakeIdWorker.nextId());
                        supplierSystemOrderCommodityDetail.setOrderId(id);
                        supplierSystemOrderCommodityDetail.setAirExpressInfoId(supplierSystemOrderCommodity.getId());
                        supplierSystemOrderCommodityDetail.setCreateId(userId);
                        supplierSystemOrderCommodityDetail.setGmtCreate(nowDateTime);
                        supplierSystemOrderCommodityDetail.setModifiedId(userId);
                        supplierSystemOrderCommodityDetail.setGmtModified(nowDateTime);
                        supplierSystemOrderCommodityDetail.setIsDeleted(IsDeletedCode.NO.getCode());
                        supplierSystemOrderCommodityDetails.add(supplierSystemOrderCommodityDetail);
                    });
                    supplierSystemOrderCommodityDetailMapper.insertBatchSomeColumn(supplierSystemOrderCommodityDetails);
                }
            });
            supplierSystemOrderCommodityMapper.insertBatchSomeColumn(supplierSystemOrderCommodityList);
        }
    }

    @Override
    public void update(Long userId, LocalDateTime nowDateTime, List<SupplierSystemOrderCommodityEditReq> supplierSystemOrderCommodityEditReqList) {
        if (CollectionUtils.isNotEmpty(supplierSystemOrderCommodityEditReqList)) {
            supplierSystemOrderCommodityEditReqList.forEach(commodityReq -> {
                UpdateWrapper<SupplierSystemOrderCommodity> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(SupplierSystemOrderCommodity::getId, commodityReq.getId());
                updateWrapper.lambda().set(SupplierSystemOrderCommodity::getCustomerRemark, commodityReq.getCustomerRemark());
                updateWrapper.lambda().set(SupplierSystemOrderCommodity::getSupplierRemark, commodityReq.getSupplierRemark());
                updateWrapper.lambda().set(SupplierSystemOrderCommodity::getModifiedId, userId);
                updateWrapper.lambda().set(SupplierSystemOrderCommodity::getGmtModified, nowDateTime);
                supplierSystemOrderCommodityMapper.update(new SupplierSystemOrderCommodity(), updateWrapper);
                commodityReq.getSupplierSystemOrderCommodityDetailEditReqList().forEach(commodityDetailReq -> {
                    UpdateWrapper<SupplierSystemOrderCommodityDetail> updateDetailWrapper = new UpdateWrapper<>();
                    updateDetailWrapper.lambda().eq(SupplierSystemOrderCommodityDetail::getId, commodityDetailReq.getId());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderCommodityDetail::getPrice, commodityDetailReq.getPrice());
                    updateDetailWrapper.lambda().set(SupplierSystemOrderCommodityDetail::getModifiedId, userId);
                    updateDetailWrapper.lambda().set(SupplierSystemOrderCommodityDetail::getGmtModified, nowDateTime);
                    supplierSystemOrderCommodityDetailMapper.update(null, updateDetailWrapper);
                });
            });
        }
    }

    @Override
    public List<SupplierSystemOrderCommodityInfoDTO> selectByOrderId(Long id) {
        QueryWrapper<SupplierSystemOrderCommodity> trainQueryWrapper = new QueryWrapper<>();
        trainQueryWrapper.lambda().eq(SupplierSystemOrderCommodity::getOrderId, id);
        List<SupplierSystemOrderCommodity> supplierSystemOrderCommodityList = supplierSystemOrderCommodityMapper.selectList(trainQueryWrapper);
        List<SupplierSystemOrderCommodityInfoDTO> supplierSystemOrderCommodityInfoDTOList = EntityUtil.copyList(supplierSystemOrderCommodityList, SupplierSystemOrderCommodityInfoDTO.class);
        if (CollectionUtils.isNotEmpty(supplierSystemOrderCommodityInfoDTOList)) {
            supplierSystemOrderCommodityInfoDTOList.forEach(trainCheckinDTO -> {
                //添加旅客
                QueryWrapper<SupplierSystemOrderCommodityDetail> trainTouristQueryWrapper = new QueryWrapper<>();
                trainTouristQueryWrapper.lambda().eq(SupplierSystemOrderCommodityDetail::getOrderId, id);
                trainTouristQueryWrapper.lambda().eq(SupplierSystemOrderCommodityDetail::getAirExpressInfoId, trainCheckinDTO.getId());
                List<SupplierSystemOrderCommodityDetail> supplierSystemOrderCommodityDetails = supplierSystemOrderCommodityDetailMapper.selectList(trainTouristQueryWrapper);
                List<SupplierSystemOrderCommodityDetailInfoDTO> supplierSystemOrderCommodityDetailInfoDTOS = EntityUtil.copyList(supplierSystemOrderCommodityDetails, SupplierSystemOrderCommodityDetailInfoDTO.class);
                trainCheckinDTO.setSupplierSystemOrderCommodityDetailInfoDTOList(supplierSystemOrderCommodityDetailInfoDTOS);
            });
        }
        return supplierSystemOrderCommodityInfoDTOList;
    }
}
